package com.example.spring_boot0815.service;


import com.example.spring_boot0815.mapper.UserMapper;
import com.example.spring_boot0815.model.Userinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.NESTED)
    public int add(Userinfo userinfo) {

        int result = userMapper.add(userinfo);
        System.out.println("add result ->" + result);
        insert(userinfo);
        return result;
    }

    @Transactional(propagation = Propagation.NESTED)
    public int insert(Userinfo userinfo) {

        int result = userMapper.add(userinfo);
        System.out.println("insert result ->" + result);
        try {
            int num = 10/0;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return result;
    }
}
